﻿Namespace Buddies
    Friend Class FEditBuddy
        Private mBuddy As Entities.Buddy
        Private mGroups As Generic.List(Of Entities.SecurityGroup)

        Public Shadows Sub Show(ByVal owner As System.Windows.Forms.Form, ByVal buddy As Entities.Buddy, ByVal options As EditBuddyOptions)
            Me.Icon = Framework.My.Resources.Buddy
            Me.Text = String.Format("{0} - Edit Buddy - Buddy 2 Buddy", buddy.NameLocal)
            mBuddy = buddy
            With buddy
                txtLocalId.Text = .Id.ToString
                txtUniqueId.Text = .UniqueId
                txtEmail.Text = .Email
                txtLastConnected.Text = .LastConnected.ToLocalTime.ToString("g")
                txtName.Text = .Name

                txtNameLocal.Text = .NameLocal
                txtComment.Text = .Comment
                txtHostName.Text = .Host
                txtPort.Text = .Port.ToString
            End With
            For Each clsGroup As Entities.SecurityGroup In Security.Handler.GetAllByBuddyId(buddy.Id)
                AddGroup(clsGroup)
            Next
            If Not options.AllowGroupMembershipEdit Then
                gbGroupMemberShips.Visible = False
                Me.Width -= gbGroupMemberShips.Right - gbInfo.Right
            End If
            If options.ReadOnly Then
                txtNameLocal.ReadOnly = True
                txtComment.ReadOnly = True
                txtHostName.ReadOnly = True
                txtPort.ReadOnly = True
                btnOK.Visible = False
                btnCancel.Text = "Close"
            End If
            MyBase.Show()
        End Sub

        Private Sub AddGroup(ByVal group As Entities.SecurityGroup)
            With lvwGroupMembership.Items.Add(group.Name)
                .SubItems.Add(group.Type.ToString)
                .SubItems.Add(group.Description)
                .Tag = group
            End With
        End Sub

        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            Try
                mBuddy.NameLocal = txtNameLocal.Text
                mBuddy.Comment = txtComment.Text
                mBuddy.Host = txtHostName.Text
                mBuddy.Port = Integer.Parse(txtPort.Text)

                Using t As New Data.TransactionScope
                    Security.Handler.SetBuddyMembersList(mBuddy.Id, GetSelectedGroupIds.ToList, t)
                    Handler.Save(mBuddy, t)

                    t.Complete()
                End Using
                Me.Close()
            Catch ex As Exception
                Framework.Misc.HandleException(ex)
            End Try
        End Sub

        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.Close()
        End Sub

        Private Sub btnAddGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGroup.Click
            Try
                Dim clsOpts As New Framework.Security.SelectGroupOptions
                clsOpts.Caption = String.Format("Select new group for {0}", txtNameLocal.Text)
                clsOpts.HideSecurityGroupIds = GetSelectedGroupIds()
                For Each clsG As Entities.SecurityGroup In Framework.Security.Manager.SelectGroups(Me, clsOpts)
                    AddGroup(clsG)
                Next
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub
        Private Function GetSelectedGroupIds() As Generic.HashSet(Of Integer)
            Dim aRet As New Generic.HashSet(Of Integer)
            For Each clsItem As System.Windows.Forms.ListViewItem In lvwGroupMembership.Items
                Dim clsG As Entities.SecurityGroup = CType(clsItem.Tag, Entities.SecurityGroup)
                aRet.Add(clsG.Id)
            Next
            Return aRet
        End Function

        Private Sub btnRemoveGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveGroup.Click
            Try
                While lvwGroupMembership.SelectedItems.Count > 0
                    Dim clsItem As System.Windows.Forms.ListViewItem = lvwGroupMembership.SelectedItems(0)
                    Dim clsG As Entities.SecurityGroup = CType(clsItem.Tag, Entities.SecurityGroup)
                    If clsG.Type = Entities.SecurityGroupType.System Then
                        System.Windows.Forms.MessageBox.Show(Me, "System groups cannot be removed!", "Remove security group", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)
                        Exit While
                    End If
                    clsItem.Remove()
                End While
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub
    End Class
End Namespace